/**
 * jims
 */
package com.jims.master.modules.drug.service;

import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.CrudService;
import com.jims.master.common.web.impl.BaseDto;
import com.jims.master.modules.drug.dao.DrugImportMasterDao;
import com.jims.master.modules.drug.dao.DrugStorageDeptDao;
import com.jims.master.modules.drug.entity.DrugImportMaster;
import com.jims.master.modules.drug.entity.DrugStock;
import com.jims.master.modules.drug.entity.DrugStorageDept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 入库主记录Service
 * @author zhuq
 * @version 2017-03-16
 */
@Service
@Transactional(readOnly = true)
public class DrugImportMasterService extends CrudService<DrugImportMasterDao, DrugImportMaster> {

    @Autowired
    private DrugStorageDeptDao drugStorageDeptDao;
    @Autowired
    private DrugImportMasterDao drugImportMasterDao;

	public DrugImportMaster get(String id) {
		return super.get(id);
	}
	
	public List<DrugImportMaster> findList(DrugImportMaster drugImportMaster) {
		return super.findList(drugImportMaster);
	}
	
	public Page<DrugImportMaster> findPage(Page<DrugImportMaster> page, DrugImportMaster drugImportMaster) {
		return super.findPage(page, drugImportMaster);
	}
	
	@Transactional(readOnly = false)
	public int save(DrugImportMaster drugImportMaster) {
		return super.save(drugImportMaster);
	}
	
	@Transactional(readOnly = false)
	public int delete(DrugImportMaster drugImportMaster) {
		return super.delete(drugImportMaster);
	}

    //根据传来的drugStock数据组装成drugImportMaster数据
    public DrugImportMaster createData(DrugStock drugStock){
        DrugImportMaster drugImportMaster=new DrugImportMaster();
        drugImportMaster.setDocumentNo(drugStock.getDocumentNo());		    // 入库单号
        drugImportMaster.setStorageId(drugStock.getStorageId());		    // 库存单位
        drugImportMaster.setImportDate(new Date());		                    // 入库日期
        if(drugStock.getImportClass().equals("采购")){
            drugImportMaster.setSupplier(drugStock.getStorageFrom());		        // 供货方
        }else{
            drugImportMaster.setStorageFrom(drugStock.getStorageFrom());
        }

        drugImportMaster.setImportClass(drugStock.getImportClass());		// 入库类别
        drugImportMaster.updateOrgId();
        drugImportMaster.preInsert();
        return drugImportMaster;
    }


    /*
    * 根据传来的drugImportMaster数据生成入库单据号
    * @ZhuQ
    * */
    public String createDocumentNo (DrugImportMaster drugImportMaster){

        //根据库房获取入库单前缀
        DrugStorageDept drugStorageDept=drugStorageDeptDao.get(drugImportMaster.getStorageId());
        String documentNo=drugStorageDept!=null?drugStorageDept.getImportNoPrefix():"";
        //获取当前日期
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
        documentNo=documentNo+ sdf.format(d);
        drugImportMaster.setDocumentNo(documentNo);
        //序号
        int i = dao.findDocumentNo(drugImportMaster);
        String num = String.valueOf(i);
        String str ="0000";
        str=str.substring(0, 4-num.length())+num;
        documentNo=documentNo+str;
        return documentNo;
    }
    /**
     * 入库记录查询
     * @param
     * @param drugImportMaster
     * @return
     */
    public Page<DrugImportMaster> findImportList(Page<DrugImportMaster> page, DrugImportMaster drugImportMaster) {
        drugImportMaster.updateOrgId();
        drugImportMaster.setPage(page);
        page.setList(drugImportMasterDao.findImportList(drugImportMaster));
        return page;
    }

    /**
     * 药出库数据统计
     * @param importDate,orgId
     * @return
     * @author ZhuQ
     */
    public List<BaseDto> drugImportCount(String importDate,String orgId){
        return drugImportMasterDao.drugImportCount(importDate,orgId);
    }
	
}